/*
 * ActorEventTest.hpp
 *
 *  Created on: 09/02/2012
 *      Author: Tales
 */

#ifndef ACTOREVENTTEST_HPP_
#define ACTOREVENTTEST_HPP_

#include "Test.hpp"
#include "Engine.hpp"
#include "errors.hpp"
#include "Actor.hpp"
#include "Event.hpp"
#include "DrawEvent.hpp"
#include "CollisionEvent.hpp"

using wolf::Engine;

namespace wolf {

class ActorEventTest: public Test {
public:
	ActorEventTest() :
			Test("Actor Event Test") {
	}

	bool run() {
		const int SCREEN_WIDTH = 800;
		const int SCREEN_HEIGHT = 600;
		Engine engine(Engine::DEBUG);
		engine.setVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0);
		engine.setFrameRate(30);
		//Creating
		engine.start();

		FrameCounterActor actor1(150, 150);
//		engine.addLayer(actor1);
		actor1.setVelX(4);

		ReturnerActor actor2(SCREEN_WIDTH - 64, 150, "right");
//		engine.addLayer(actor2);
		actor2.setX(actor2.getX() - actor2.getSprite().getWidth());

		ReturnerActor actor3(64, 150, "left");
//		engine.addLayer(actor3);

		while (engine.isRunning()) {
			engine.doFrame();
		}
//		engine.end();
		return true;
	}

	class FrameCounterActor: public Actor {
	public:
		FrameCounterActor(int x, int y) :
				Actor("resource/spritetest.png", 4, 1, x, y) {
			lifeInFrames = 0;
			frameStartCount = 0;
			frameEndCount = 0;
			lastCollision = "No collision";
			connectFrameEvent(&FrameCounterActor::onFrame);
			connectStartFrameEvent(&FrameCounterActor::onStartFrame);
			connectEndFrameEvent(&FrameCounterActor::onEndFrame);
			connectDrawEvent(&FrameCounterActor::onDraw);
			connectCollisionEvent(&FrameCounterActor::onCollision);
		}

		void onDraw(DrawEvent &event) {
			SDL_Surface *screen = event.getScreen();
			Engine &engine = event.getEngine();
			drawSprite(screen);
			StdFont font;
			stringstream s;
			s << "I lived by " << lifeInFrames / engine.getFrameRate()
					<< " seconds";
			font.writeText(s.str(), screen, 0, 0, Color(0xffFFffFF));
			s.str("");
			s << "Frame Starts : " << frameStartCount;
			font.writeText(s.str(), screen, 0, 8, Color(0xffFFffFF));
			s.str("");
			s << "Frame Ends   : " << frameEndCount;
			font.writeText(s.str(), screen, 0, 16, Color(0xffFFffFF));
			s.str("");
			s << "LastCollision: " << lastCollision;
			font.writeText(s.str(), screen, 0, 24, Color(0xffFFffFF));
		}

		void onFrame(Event &event) {
			++lifeInFrames;
		}

		void onStartFrame(Event &event) {
			++frameStartCount;
		}

		void onEndFrame(Event &event) {
			++frameEndCount;
		}

		void onCollision(CollisionEvent &event) {
			if (ReturnerActor *other = dynamic_cast<ReturnerActor*>(&event.getOther()))
				lastCollision = other->position;
			else
				lastCollision = "Unknown";
		}
	private:
		int lifeInFrames;
		int frameStartCount;
		int frameEndCount;
		string lastCollision;
	};

	class ReturnerActor: public Actor {
	public:
		ReturnerActor(int x, int y, const string &position) :
				Actor(string("resource/actortest_") + position + ".png", 1, 1,
						x, y), position(position) {
			connectCollisionEvent(&ReturnerActor::onCollision);
		}
		string position;

		void onCollision(CollisionEvent &event) {
			Actor &other = event.getOther();
			other.setVelX(-other.getVelX());
		}
	};
};

} /* namespace yep */
#endif /* ACTOREVENTTEST_HPP_ */
